<!DOCTYPE html>
<html>
<head>
    <title>锻炼管理</title>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1, user-scalable=no">
    <meta name="description" content="木兰湾管理系统">
    <script type="text/javascript" src="../../static/include/header-css.js"></script>
</head>
<body>
<div>
    <form id="search-form">
        <div class="weui-cell mulanbay_select-before ">
            <div class="weui-cell__hd"><label class="weui-label">运动类型</label></div>
            <div class="weui-cell__bd">
                <input class="weui-input" id="sportTypeSearchList" type="text" value="">
            </div>
            <div class="weui-cell__hd">
                <a href="javascript:searchData()" class="weui-btn weui-btn_mini weui-btn_primary">查询</a>
            </div>
        </div>
        <input type="hidden" name='sportTypeId' id="sportTypeSearchId"/>
        <input type="hidden" name='page' value="1" id="page"/>
        <input type="hidden" name='sortField' value="EXERCISEDATE" />
        <input type="hidden" name='sortType' value="desc" />
    </form>
</div>
<!--列表数据显示-->
<div id="dataContent"></div>
<!--滚动加载-->
<div class="weui-loadmore" id="loadingSpan">
    <span class="weui-loadmore__tips">加载中...</span>
</div>
<!--编辑页面-->
<div id="edit-popup" class='weui-popup__container'>
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
        <div class="toolbar">
            <div class="toolbar-inner">
                <a href="javascript:;" class="picker-button close-popup">关闭</a>
                <h1 class="title">锻炼信息</h1>
            </div>
        </div>
        <div class="modal-content">
            <div class="white_bg">
                <form id="edit-form">
                    <div class="weui-cell mulanbay_select-before ">
                        <div class="weui-cell__hd" ><label class="weui-label">运动类型<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" id="sportTypeList" type="text" value="" >
                            <input type="hidden" name='sportTypeId' id="sportTypeId" value=""/>
                        </div>
                        <div class="weui-cell__hd">
                            <a href="javascript:showAddSportType();"><img src="../../static/image/add.png" alt="说明"></img></a>
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">锻炼时间<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" id='exerciseDate' name="exerciseDate" type="text" value="">
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">锻炼时长<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="minutes">
                        </div>
                        <div class="weui-cell__hd">
                            分钟
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">锻炼值<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="kilometres">
                        </div>
                        <div class="weui-cell__hd">
                            单位:<label id="unitName"></label>
                        </div>
                    </div>
                    <div class="weui-cells__title" align="center"><<<--更多选项-->>></div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">平均配速</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="pace">
                        </div>
                        <div class="weui-cell__hd">
                            分钟/公里
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">最佳配速</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="maxPace">
                        </div>
                        <div class="weui-cell__hd">
                            分钟/公里
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">平均速度</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="speed">
                        </div>
                        <div class="weui-cell__hd">
                            公里/小时
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">最佳速度</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="maxSpeed">
                        </div>
                        <div class="weui-cell__hd">
                            公里/小时
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">最大心率</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="maxHeartRate">
                        </div>
                        <div class="weui-cell__hd">
                            次
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">平均心率</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="number" placeholder="" name="averageHeartRate">
                        </div>
                        <div class="weui-cell__hd">
                            次
                        </div>
                    </div>
                    <div class="weui-cells__title"><span class='mulanbay_textarea_title'>备注</span></div>
                    <div class="weui-cells weui-cells_form">
                        <div class="weui-cell">
                            <div class="weui-cell__bd">
                                <textarea class="weui-textarea" placeholder="" rows="3" name="remark"></textarea>
                                <div class="weui-textarea-counter"><span>0</span>/200</div>
                            </div>
                        </div>
                    </div>
                    <input type="text" style="display:none;" name='id' id="id" value=""/>
                </form>
            </div>
            <br>
            <br>
            <a href="javascript:saveData();" class="weui-btn weui-btn_primary weui-footer_fixed-bottom"><label id="edit-button-name">保存</label></a>
        </div>
    </div>
</div>

<!--运动类型编辑页面-->
<div id="edit-sportType-popup" class='weui-popup__container popup-bottom'>
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
        <div class="toolbar">
            <div class="toolbar-inner">
                <a href="javascript:$.openPopup('#edit-popup');" class="picker-button close-popup">关闭</a>
                <h1 class="title">运动类型</h1>
            </div>
        </div>
        <div class="modal-content">
            <div class="white_bg">
                <form id="edit-sportType-form">
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd">
                            <label class="weui-label">名称<span class="mulanbay-notNull">*</span></label>
                        </div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="text" placeholder="" name="name">
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd">
                            <label class="weui-label">单位<span class="mulanbay-notNull">*</span></label>
                        </div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="text" placeholder="" name="unit">
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">状态<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-switch" type="checkbox" name="status" id="status" data-on-text="true" data-off-text="false">
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">排序号</label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" type="text" placeholder="" name="orderIndex">
                        </div>
                    </div>
                    <input type="text" style="display:none;" name='id' id="crId" value=""/>
                </form>
            </div>
            <br>
            <br>
            <a href="javascript:saveSportTypeData();" class="weui-btn weui-btn_primary weui-footer_fixed-bottom"><label>保存</label></a>
        </div>
    </div>
</div>

<!--以模板新增页面-->
<div id="edit-addFromTmp-popup" class='weui-popup__container popup-bottom'>
    <div class="weui-popup__overlay"></div>
    <div class="weui-popup__modal">
        <div class="toolbar">
            <div class="toolbar-inner">
                <a href="javascript:$.closePopup('#edit-addFromTmp-popup');" class="picker-button close-popup">关闭</a>
                <h1 class="title">以模板新增</h1>
            </div>
        </div>
        <div class="modal-content">
            <div class="white_bg">
                <form id="edit-addFromTmp-form">
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">模板日期<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" id='templateDate' name="templateDate" type="text" value="">
                        </div>
                    </div>
                    <div class="weui-cell mulanbay_select-before">
                        <div class="weui-cell__hd"><label class="weui-label">开始时间<span class="mulanbay-notNull">*</span></label></div>
                        <div class="weui-cell__bd">
                            <input class="weui-input" id='beginTime' name="beginTime" type="text" value="">
                        </div>
                    </div>
                </form>
            </div>
            <br>
            <br>
            <a href="javascript:saveByTemplate();" class="weui-btn weui-btn_primary weui-footer_fixed-bottom"><label>保存</label></a>
        </div>
    </div>
</div>

<br>
<a href="javascript:showAdd();" class="weui-btn weui-btn_primary weui-footer_fixed-bottom">新增</a>
<script type="text/javascript" src="../../static/include/footer-js.js"></script>
<script>
    $(function() {
        pushRefresh(getData);
        datetimePicker('exerciseDate',null);
        datetimePicker('beginTime',null);
        $("#templateDate").calendar({dateFormat: 'yyyy-mm-dd'});
        getData();
        pushStateListener();
        loadSportTypeSearchList();
    });
    function showAddSportType() {
        $.openPopup('#edit-sportType-popup') ;
    }
    function showAddFromTmp(exerciseDate) {
        $.openPopup('#edit-addFromTmp-popup') ;
        var formData = {
            beginTime: getNowDateTimeString(),
            templateDate:exerciseDate.substring(0,10)
        };
        $('#edit-addFromTmp-popup').loadJson(formData);
    }
    function saveSportTypeData() {
        var postData = form2Json("edit-sportType-form");
        var status = $("#status").is(":checked");
        if(status==true){
            postData.status='ENABLE';
        }else{
            postData.status='DISABLE';
        }
        var url='/sportType/create';
        doFormSubmit(postData,url,function(data){
            $.closePopup('#edit-sportType-popup') ;
            $.openPopup('#edit-popup') ;
            loadSportTypeList(false,true);
        });
    }
    function saveByTemplate() {
        var postData = form2Json("edit-addFromTmp-form");
        var create=false;
        var url='/sportExercise/createByTemplate';
        doFormSubmit(postData,url,function(data){
            $.closePopup('#edit-addFromTmp-popup') ;
            getData();
        });
    }
    function showAdd() {
        $.openPopup('#edit-popup') ;
        initForm(true,true);
        clearForm("edit-form");
        var formData = {
            exerciseDate:getNowDateTimeString(),
            speed: 0,
            maxSpeed:0,
            pace:0,
            maxPace:0,
            maxHeartRate:0,
            averageHeartRate : 0
        };
        $('#edit-form').loadJson(formData);
        setElementInnerHTML('edit-button-name','增加');
    }
    function showEdit(id) {
        var url='/sportExercise/get?id='+ id;
        doAjax(null,url,'GET',false,function(data){
            $.openPopup('#edit-popup') ;
            clearForm("edit-form");
            initForm(false,false);
            data.sportTypeId=data.sportType.id;
            $('#edit-form').loadJson(data);
            document.getElementById('sportTypeList').value=data.sportType.name;
            setElementInnerHTML('unitName',data.sportType.unit);
        });
        setElementInnerHTML('edit-button-name','修改');
    }
    function initForm(selectFirst,async) {
        loadSportTypeList(selectFirst,async);
    }
    function loadSportTypeSearchList(){
        var url='/sportType/getSportTypeTree?needRoot=true';
        loadSelectList(url,'sportTypeSearchList','sportTypeSearchId','请选择类型',false,true,function (ov,nv) {
            searchData();
        },null);
    }
    function loadSportTypeList(selectFirst,async){
        var url='/sportType/getSportTypeTree';
        loadSelectList(url,'sportTypeList','sportTypeId','请选择类型',selectFirst,async,function (ov,nv) {
            loadSportType(nv);
        },null);
    }
    function loadSportType(id) {
        if(id==null||id==''){
            return;
        }
        if($('#sportTypeId').val()==id){
            //return;
        }
        var url='/sportType/get?id='+ id;
        doAjax(null,url,'GET',false,function(data) {
            $('#name').val(data.name);
            setElementInnerHTML('unitName',data.unit);
        });
    }
    function saveData() {
        var postData = form2Json("edit-form");
        var create=false;
        var url='/sportExercise/edit';
        if($("#id").val()==null||$("#id").val()==''){
            url='/sportExercise/create';
            create=true;
        }
        doFormSubmit(postData,url,function(data){
            $.closePopup('#edit-popup') ;
            if(create){
                getData();
                gotoTop();
            }else {
                var hh = createRow(data);
                setElementInnerHTML(getDivId(data.id),hh);
            }
        });
    }
    function showDelete(id) {
        var delUrlPrefix = '/sportExercise/delete';
        commonDeleteByIds(delUrlPrefix,id,function () {
            //searchData();
            //直接把该节点删除
            setElementInnerHTML(getDivId(id),'');
        });
    }
    function searchData() {
        setPage(1);
        getData();
    }
    function getData() {
        var url = '/sportExercise/getData';
        loadListData('search-form','dataContent','page',url,createRow,null);
    }
    function getDivId(id) {
        return 'SportExercise'+id;
    }
    function createRow(rowData) {
        var html='';
        html+='<div id="'+getDivId(rowData.id)+'">';
        html+='<div class="weui-form-preview">';
        html+='    <a class="weui-cell  weui-cell_access weui-cell_example" href="javascript:showEdit('+rowData.id+');">\n';
        var pic = '<img src="../../static/image/sport.png">';
        html+='        <div class="weui-cell__hd">'+pic+'</div>\n';
        html+='        <div class="weui-cell__bd">\n';
        html+='<label class="weui-form-preview__label mulanbay-dark-font">'+rowData.sportType.name+'</label>';
        html+='        </div>\n';
        html+='        <div class="weui-cell__ft">'+rowData.minutes+'分钟</div>\n';
        html+='    </a>\n';
        html+='    <div class="weui-form-preview__bd">\n';
        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">锻炼时间</label>\n';
        html+='            <span class="weui-form-preview__value">'+rowData.exerciseDate+'</span>\n';
        html+='        </div>\n';
        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">数值</label>\n';
        var vv = rowData.kilometres+'('+rowData.sportType.unit+')';
        if ('CURRENT' ==rowData.mileageBest) {
            vv = '<font color="red" alt="aaaa">★</font>'+vv;
        }else if ('ONCE' ==rowData.mileageBest) {
            vv = '<font color="purple">☆</font>'+vv;
        }
        html+='            <span class="weui-form-preview__value">'+vv+'</span>\n';
        html+='        </div>\n';

        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">平均速度</label>\n';
        var vv = rowData.speed+'(公里/小时)';
        if ('CURRENT' ==rowData.fastBest) {
            vv = '<font color="red" alt="aaaa">★</font>'+vv;
        }else if ('ONCE' ==rowData.fastBest) {
            vv = '<font color="purple">☆</font>'+vv;
        }
        html+='            <span class="weui-form-preview__value">'+vv+'</span>\n';
        html+='        </div>\n';

        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">最佳速度</label>\n';
        html+='            <span class="weui-form-preview__value">'+rowData.maxSpeed+'(公里/小时)</span>\n';
        html+='        </div>\n';
        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">平均配速</label>\n';
        html+='            <span class="weui-form-preview__value">'+rowData.pace+'(分钟/公里)</span>\n';
        html+='        </div>\n';
        html+='        <div class="weui-form-preview__item">\n';
        html+='            <label class="weui-form-preview__label">最佳配速</label>\n';
        html+='            <span class="weui-form-preview__value">'+rowData.maxPace+'(分钟/公里)</span>\n';
        html+='        </div>\n';
        html+='        <div class="weui-form-preview__item">\n';
        var mhr = rowData.maxHeartRate;
        if(mhr >=rowData.safeMaxHeartRate) {
            mhr = '<font color="red">'+mhr+'</font>';
        }
        html+='            <label class="weui-form-preview__label">最大心率</label>\n';
        html+='            <span class="weui-form-preview__value">'+mhr+'</span>\n';
        html+='        </div>\n';
        html+='        <div class="weui-form-preview__item">\n';
        var ahr = rowData.averageHeartRate;
        if(ahr >=rowData.safeMaxHeartRate*0.8) {
            ahr = '<font color="red">'+ahr+'</font>';
        }
        html+='            <label class="weui-form-preview__label">平均心率</label>\n';
        html+='            <span class="weui-form-preview__value">'+ahr+'</span>\n';
        html+='        </div>\n';
        html+='    </div>\n';
        html+='    <div class="weui-form-preview__ft">\n';
        html+='        <a class="weui-form-preview__btn weui-form-preview__btn_green " href="javascript:showAddFromTmp(\''+rowData.exerciseDate+'\');">以该天为模板</a>\n';
        html+='        <a class="weui-form-preview__btn weui-form-preview__btn_delete" href="javascript:showDelete('+rowData.id+');">删除</a>\n';
        html+='    </div>\n';
        html+='</div>\n';
        html+='</div>\n';
        html+='<br>\n';
        return html;
    }
</script>
</body>
</html>